package de.projectgoodmorning.alarm.activities;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import de.projectgoodmorning.alarm.AlarmController;
import de.projectgoodmorning.alarm.R;
import de.projectgoodmorning.alarm.model.Configuration;
import de.projectgoodmorning.alarm.storage.ConfigStorage;
import de.projectgoodmorning.alarm.util.AlarmFactory;

/**
 * Die RingAlarmActivity wird aufgerufen wenn ein RingAlarm durch den
 * AlarmController ausgefuehrt wird. 
 * 
 * Diese Activity ermoeglicht dem Nutzer das Wecksignal zu beenden oder
 * die Snooze-Funktion zu aktivieren.
 */
public class RingAlarmActivity extends Activity implements OnClickListener {
	private static final String TAG = RingAlarmActivity.class.getName();

	private MediaPlayer player;
	private Button stop, snooze;

	private AlarmController alarmController;
//	private PlaySoundService playService;
	private ConfigStorage configStorage;

	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_ringalarm );

		stop = ( Button ) findViewById( R.id.ringalarmStopButton );
		snooze = ( Button ) findViewById( R.id.ringalarmSnoozeButton );

		stop.setOnClickListener( this );
		snooze.setOnClickListener( this );

		configStorage = new ConfigStorage( this );
		
		bindService( new Intent( this, AlarmController.class ), connection,
				Context.BIND_AUTO_CREATE );

//		startService( new Intent( this, PlaySoundService.class ) );
//		
//		bindService( new Intent( this, PlaySoundService.class ), playConnection,
//				Context.BIND_AUTO_CREATE );

		// Wake Up
		PowerManager pm = ( PowerManager ) getSystemService( Context.POWER_SERVICE );
		WakeLock wl = pm.newWakeLock( 
				PowerManager.SCREEN_BRIGHT_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG );
		wl.acquire();
		
		// Disable KeyGuard
		KeyguardManager km = ( KeyguardManager ) getSystemService( KEYGUARD_SERVICE );
		km.newKeyguardLock( TAG ).disableKeyguard();
		
		Configuration config = configStorage.getConfig();

		try {
			player = MediaPlayer.create( this, config.ringtoneAddress );
			player.setLooping( true );
			player.start();
			
		} catch( Exception e ) {
			e.printStackTrace();
			
			// Fallback -> if default ringtone is not available
			player = MediaPlayer.create( this, R.raw.church );
			player.setLooping( true );
			player.start();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		player.reset();
		player.release();
		
		unbindService( connection );
//		unbindService( playConnection );
	}
	
	@Override
	protected void onResume() {
		super.onResume();

		if( alarmController == null ) {
			bindService( new Intent( this, AlarmController.class ), connection,
					Context.BIND_AUTO_CREATE );
		}
		
//		if( playService == null ) {
//			bindService( new Intent( this, PlaySoundService.class ), playConnection,
//					Context.BIND_AUTO_CREATE );
//		}
	}

	@Override
	public void onClick( View v ) {
		if( v == stop ) {
			stopAndFinish();
		}

		if( v == snooze ) {
			Configuration conf = configStorage.getConfig();
			int snoozeSecs = conf.snoozeTimeInMinutes * 60;
			alarmController.addAlarm( AlarmFactory.createRingAlarm( snoozeSecs, this ) );
			stopAndFinish();
		}
	}

	private void stopAndFinish() {
		try {
			player.stop();
//			playService.stopPlayer();
		} catch( Exception e ) {
		}
		
		finish();
	}

	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected( ComponentName name ) {
			alarmController = null;
		}

		@Override
		public void onServiceConnected( ComponentName name, IBinder service ) {
			alarmController = ( ( AlarmController.LocalBinder ) service ).getService();
		}
	};
	
//	private ServiceConnection playConnection = new ServiceConnection() {
//		@Override
//		public void onServiceDisconnected( ComponentName name ) {
//			playService = null;
//		}
//		
//		@Override
//		public void onServiceConnected( ComponentName name, IBinder service ) {
//			playService = ( ( PlaySoundService.LocalBinder ) service ).getService();
//			Log.d( TAG, "PlayService started" );
//		}
//	};
}
